//
//  HJBlackListVC.m
//  project
//
//  Created by jc on 2019/5/6.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJBlackListVC.h"

@interface HJBlackListVC ()
@property (nonatomic,strong) NSMutableArray <HJUserModel *>* blackList;
@end
static NSString * const identifier = @"HJBlackListCell";
@implementation HJBlackListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"黑名单";
    [self loadNetworkData];
}
-(NSMutableArray <HJUserModel *>*)blackList {
    if (!_blackList) {
        _blackList = [NSMutableArray array];
    }
    return _blackList;
}

-(void)loadNetworkData {
    NSString * url = [Api stringByAppendingString:MyUserBlackListApi];
    [HJHttpTool get:url params:@{@"pageCurrent":@(self.page),@"userId":[HJUserManager getUserModel].userId,@"pageSize":@"20"} success:^(HJResponeds *result) {
        NSArray * arr = [NSArray yy_modelArrayWithClass:[HJUserModel class] json:result.data[@"records"]];
        self.totalPage = result.data[@"pages"];
        if (self.page==1) {
            [self.blackList removeAllObjects];
        }
        [self.blackList addObjectsFromArray:arr];
        [self.tableView endRefresh];
        [self.tableView reloadData];
    } failure:^(id result) {
        [self.tableView endRefresh];
    }];
}
-(void)configTableView {
    self.needRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.rowHeight = 60;
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.tableHeaderView.height = 10;
    self.tableView.separatorInset = UIEdgeInsetsMake(43, 0, 0, 0);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.blackList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJBlackListCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[HJBlackListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell loadDataMore:self.blackList[indexPath.row]];
    @weakify(self)
    cell.removeBlock = ^(NSString * _Nonnull fromId) {
        @strongify(self)
        NSString * removeUrl =[Api stringByAppendingString:RemoveBlackUserApi];
        [HJHttpTool post:removeUrl params:@{@"friendUserId":fromId,@"userId":[HJUserManager getUserModel].userId} success:^(HJResponeds *result) {
            [self showSuccessHUDWithStr:@"移除黑名单成功"];
            [self.blackList removeObject:self.blackList[indexPath.row]];
            [tableView reloadData];
        } failure:^(id result) {
            [self showErrorHUDWithError:@"移除黑名单失败"];
        }];
    };
    return cell;
}


@end


@implementation HJBlackListCell


-(void)loadDataMore:(HJUserModel * _Nullable )model {
    self.userModel = model;
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:nil];
    self.nameLabel.text = model.nickName;
    self.contentLabel.text = model.intro;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubViews];
    }
    return self;
}

-(void)initSubViews {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    
    UIImageView * avatorImageView = [UIImageView new];
    self.mainImageView = avatorImageView;
    [self.contentView addSubview:avatorImageView];
    
    UILabel * nameLabel = [UILabel new];
    nameLabel.textColor = [UIColor colorWithHex:0x000000];
    nameLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
    self.nameLabel = nameLabel;
    [self.contentView addSubview:nameLabel];
    
    UILabel * contentLabel = [UILabel new];
    contentLabel.textColor = [UIColor colorWithHex:0x999999];
    contentLabel.font = [UIFont fontWithName:PingFangSC_Medium size:13];
    self.contentLabel = contentLabel;
    [self.contentView addSubview:contentLabel];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:setImage(@"remove") forState:0];
    [btn setTitle:@"移除黑名单" forState:0];
    btn.adjustsImageWhenHighlighted = NO;
    [btn setTitleColor:[UIColor colorWithHex:0x999999] forState:0];
    btn.titleLabel.font = [UIFont systemFontOfSize:10];
    self.removBtn = btn;
    @weakify(self)
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.removeBlock) {
            self.removeBlock(self.userModel.userId);
        }
    }];
    [self.contentView addSubview:btn];
    
}

-(void)layoutSubviews {
    [super layoutSubviews];
    @weakify(self)
    [self.mainImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.left.mas_equalTo(12);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        [self.mainImageView rounded:UIRectCornerAllCorners radius:20 borderColor:[UIColor blackColor] borderWidth:1];
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.mas_equalTo(12);
        make.left.equalTo(self.mainImageView.mas_right).with.offset(10);
        make.height.mas_equalTo(15);
    }];
    
//    [self.removBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        @strongify(self)
//        make.right.equalTo(self.contentView).with.inset(10);
//        make.centerY.equalTo(self.contentView);
//        make.size.mas_equalTo(CGSizeMake(60, 50));
//    }];
    self.removBtn.frame = CGRectMake(self.contentView.width-10-60, self.contentView.centerY-25, 60, 50);
    [self initHorizonAlignmentWithBtn:self.removBtn];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.bottom.equalTo(self.mainImageView.mas_bottom).with.inset(5);
        make.left.equalTo(self.nameLabel.mas_left);
        make.height.mas_equalTo(13);
        make.right.equalTo(self.removBtn.mas_left).with.inset(5);
    }];
}

-(void)initHorizonAlignmentWithBtn:(UIButton *)btn {
    //使图片和文字水平居中显示
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height+15 ,-btn.imageView.frame.size.width, 0.0,0.0)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(-5, btn.titleLabel.frame.size.width/2,0.0, 0)];
    
}


@end
